#include "htf_format_detector.h"
#include <string.h>

// FlatBuffers magic: 0x464C4246 (FLBF)
// Protobuf: no magic, but check for valid tag (varint < 0x80000000)
// JPEG: starts with 0xFFD8

htf_model_format_t htf_detect_format(const uint8_t* data, size_t size) {
    if (size < 4) return HTF_FMT_UNKNOWN;

    uint32_t magic = *(uint32_t*)data;
    if (magic == 0x464C4246 || magic == 0x42464C46) // little/big endian
        return HTF_FMT_FLATBUFFERS;

    if (size >= 2 && data[0] == 0xFF && data[1] == 0xD8)
        return HTF_FMT_JPEG_EMBEDDED;

    // Simple protobuf heuristic: first byte is field tag < 0x80
    if ((data[0] & 0x80) == 0)
        return HTF_FMT_PROTOBUF;

    // Assume raw float if size divisible by 4 and no magic
    if (size % 4 == 0 && size > 100)
        return HTF_FMT_RAW_FLOAT;

    return HTF_FMT_UNKNOWN;
}